package com.liangzhong.model.tradeCenter
{
	import com.common.utils.timeUtils.TimerManager;
	import com.common.utils.timeUtils.TimerUtils;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.model.tradeCenter.sales.SalesPlayerVo;

	/**
	 * 交易中心 
	 * @author Administrator
	 * 
	 */
	public class TradeCenterManager
	{
		/**
		 *球员探索更新时间  
		 */
		private var _updataTime:int;
		/**
		 *回调函数 
		 */
		private var _callBack:Function;
		/**
		 *交易中心球员Vec 
		 */
		private var _tradePlayerVec:Vector.<TradePlayerVo>;
		///////////////////////////
		///任务相关数据
		/**
		 *购买球员数目 
		 */		
		private var _buyPlayerCount	:int;
		/**
		 *市场购买球员数据 
		 */		
		public var auPlayerVo  :AuctionPlayerVo;
		
		
		public function TradeCenterManager()
		{
		}
		public function set updataTime($value:int):void
		{
			_updataTime = $value;
			if(_updataTime > 0)
				TimerManager.instance.addFunction(changeTime);
		}
		public function get updataTime():int
		{
			return _updataTime;
		}
		/**
		 *改变时间  
		 * 
		 */
		private function changeTime():void
		{
			if(_callBack != null)
			{
				var str:String = TimerUtils.changeToh_m_s(_updataTime);
				_callBack(str);
			}
			if(_updataTime >= 0)
				_updataTime--;
			else
				TimerManager.instance.removeFunction(changeTime);
//			if(_updataTime < 0 && _callBack != null)
//			{
//				_callBack(TimerUtils.changeToh_m_s(_updataTime));
//			}
		}
		/**
		 * 设置回调函数
		 * @param $callBack
		 * 
		 */
		public function setCallBackFunction($callBack:Function):void
		{
			_callBack = null;
			_callBack = $callBack;
		}
		
		/**
		 *解析交易中心球员 
		 * 
		 */
		public function analysisTradePlayer($arr:Array):void
		{
			if(_tradePlayerVec == null)_tradePlayerVec = new Vector.<TradePlayerVo>();
			var len:int = _tradePlayerVec.length;
			var tradePlayerVo:TradePlayerVo;
			while(len > 0)
			{
				tradePlayerVo = _tradePlayerVec.pop();
				tradePlayerVo = null;
				len--;
			}
			var i:int = 0;
			len = $arr.length;
			for(i = 0;i < len;i++)
			{
				tradePlayerVo = new TradePlayerVo();
				tradePlayerVo.analysisData($arr[i]);
				_tradePlayerVec.push(tradePlayerVo);
			}
		}
		/**
		 *删除球员  
		 * @param $hid
		 * 
		 */		
		public function removePlayer($hid:int):void
		{
			var len:int = _tradePlayerVec.length;
			for(var i:int = 0;i < len;i++)
			{
				if(_tradePlayerVec[i].hid == $hid)
				{
					_tradePlayerVec.splice(i,1);
					break;
				}
			}
		}
		/**
		 *获得交易球员长度 
		 * @return 
		 * 
		 */
		public function get tradePlayerLen():int
		{
			return _tradePlayerVec.length;
		}
		/**
		 *获得单个球员数据 
		 * @param $value
		 * @return 
		 * 
		 */
		public function getTradePlayerVo($value:int):TradePlayerVo
		{
			return _tradePlayerVec[$value];
		}
		public function getTradePlayerVec():Vector.<TradePlayerVo>
		{
			return _tradePlayerVec;
		}
		/**
		 *设置购买球员数量 
		 * @param type 类型 -- 1:市场交易，2：探索招募 ,3:投放,4:招募球星,5:探索球员
		 * @param value 数量
		 * @param abiliy 球员能力
		 */		
		public function setBuyPlayerCount(type :int,value :int = 1,abiliy:int = 0):void
		{
			if(type == 1 && auPlayerVo != null)
			{
				abiliy = auPlayerVo.abiliy;
			}
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.TRADE_DATA_CHANGE,{type:type,num:value,abiliy:abiliy});
		}
		
		public function get buyPlayerCount():int
		{
			return _buyPlayerCount;
		}
	}
}